package com.cloud.gateway.routes;

import com.cloud.gateway.filters.RouteFilter;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class GatewayRoutes {

    @Resource
    RouteFilter routeFilter;

    @Bean
    public RouteLocator routeLocator(RouteLocatorBuilder builder)
    {
        return builder.routes()
                .route(r->
                        r.path("/ecs/ecparty/**")
                            .and()
                            .method(HttpMethod.POST)
                            .and()
                            .readBody(String.class,readBody->true)
                            .filters(
                                    f->f.rewritePath("/ecs/ecparty/(?<segment>.*)","/$\\{segment}")
                                        .prefixPath("/ecparty").filter(routeFilter))
                            .uri("http://192.168.99.100:7070")
                             //   .uri("lb://ECPARTY")
                            .id("ecparty")
                        )
                .route(r->
                        r.path("/ecs/iuser/**")
                                .and()
                                .method(HttpMethod.POST)
                                .and()
                                .readBody(String.class,readBody->true)
                                .filters(
                                        f->f.rewritePath("/ecs/iuser/(?<segment>.*)","/$\\{segment}")
                                                .prefixPath("/iuser").filter(routeFilter))
                                .uri("http://192.168.99.100:7070")
                                //   .uri("lb://ECPARTY")
                                .id("iuser")
                )
                .build();
    }
}
